package com.队列;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 循环队列
 */
public class MyCicularQueue2 {


        private int[] data;
        private int head;
        private int tail;
        private int size;

        /** Initialize your data structure here. Set the size of the queue to be k. */
        public MyCicularQueue2(int k) {
            data = new int[k];
            head = -1;
            tail = -1;
            size = k;
        }

        /** Insert an element into the circular queue. Return true if the operation is successful. */
        public boolean enQueue(int value) {
            if (isFull() == true) {
                System.out.println("满队列");
                return false;
            }
            if (isEmpty() == true) {
            }
            head = 0;
            tail = (tail + 1) % size;
            data[tail] = value;
            return true;
        }

        /** Delete an element from the circular queue. Return true if the operation is successful. */
        public boolean deQueue() {
            if (isEmpty() == true) {
                System.out.println("空队列");
                return false;
            }
            data[head] = 0;
            if (head == tail) {

                head = -1;
                tail = -1;
                return true;
            }

            head = (head + 1) % size;
            return true;
        }

        /** Get the front item from the queue. */
        public int Front() {
            if (isEmpty() == true) {
                return -1;
            }
            return data[head];
        }

        /** Get the last item from the queue. */
        public int Rear() {
            if (isEmpty() == true) {
                return -1;
            }
            return data[tail];
        }

        /** Checks whether the circular queue is empty or not. */
        public boolean isEmpty() {
            return head == -1;
        }

        /** Checks whether the circular queue is full or not. */
        public boolean isFull() {
            return ((tail + 1) % size) == head;
        }

    @Override
    public String toString() {
        System.out.println("MyCicularQueue2{" +
                "data=" + Arrays.toString(data) +
                ", head=" + head +
                ", tail=" + tail +
                ", size=" + size +
                '}');
        return "MyCicularQueue2{" +
                "data=" + Arrays.toString(data) +
                ", head=" + head +
                ", tail=" + tail +
                ", size=" + size +
                '}';
    }

    /**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */
public static void main(String[] args) {
    MyCicularQueue2 queue = new MyCicularQueue2(5);


    int  i;
    while (true){
        // System.out.println("出队输入0");
        //System.out.println("结束输入-1");
        System.out.println("输入入队数字：");
        i = new Scanner(System.in).nextInt();
        if(i == -1){
            System.out.println("结束队列");
            break;
        }
        if(i==0){
            if(!queue.deQueue()){
                System.out.println("空队列");
            }
                queue.toString();
        }
        else{
            queue.enQueue(i);
            queue.toString();

        }

    }
}
}
